package com.tudou.searchcontrol.service.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tudou.searchcontrol.dao.AlbumDao;
import com.tudou.searchcontrol.model.Album;
import com.tudou.searchcontrol.model.PageInfo;
import com.tudou.searchcontrol.model.TudouM;
import com.tudou.searchcontrol.service.IAlbumService;
import com.tudou.searchcontrol.service.ITudouMService;


/**
 * 
 * @author zwu
 *
 */
@Service("albumService")
public class AlbumService implements IAlbumService{
	
	@Autowired
	private ITudouMService tudouMService;
	
	@Autowired
	private AlbumDao albumDao;
	
	public List<TudouM> getAlbumList(PageInfo pageInfo){
		pageInfo = tudouMService.getTudouMByPage(new HashMap(), pageInfo);
		List<TudouM> list  = new ArrayList<TudouM>();
		if(pageInfo != null){
			list = pageInfo.getDataList();
		}
		
		return list;
	}

	public List<Album> getAlbumList(String url){
		List<Album> list = new ArrayList<Album>();
		SAXBuilder reader = new SAXBuilder();
		try {
			Document doc = reader.build(url);
			Element root = doc.getRootElement();
			List xmlList= root.getChildren("url");
			for(int i=0;i<xmlList.size();i++){
				Album album = new Album();
				Element element = (Element)xmlList.get(i);
				Element childElement = element.getChild("data").getChild("display");
//				album.setTitle(childElement.getChildText("workName"));
				list.add(album);
			}
			
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	public boolean getImgSrc(String content){
	    Pattern p = Pattern.compile("<img.*?>", Pattern.CASE_INSENSITIVE);  
        Matcher m = p.matcher(content);  
        boolean istudou = false;
        while (m.find()) {  
           if(m.group().indexOf("http://static.soku.com/v1.0.0688/soku/img/favicon/tudou.png")>0){
        	   istudou = true;
        	   break;
           }
        } 
        return istudou;
	 }
	
	
	public String savexmlUrl(String xmlUrl) {
		try{
		    URL url = new URL(xmlUrl);
		    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		    conn.connect();
		    InputStream stream = conn.getInputStream();
		    BufferedReader reader = new BufferedReader(new InputStreamReader(
		      stream, "UTF-8"));
		    StringBuffer document = new StringBuffer();
		    String line = null;
		    while ((line = reader.readLine()) != null) {
		    	document.append(line);
		    }
		    return document.toString();
		}catch (Exception e) {
			System.out.println(e+"||"+xmlUrl);
	    }
		return "";
	}
	
	
	public static void main(String[] args) {
		List<String> albumList = new ArrayList<String>();
		albumList.add("FF");
		albumList.add("FFxxx");
		try{
			FileWriter fw=new FileWriter(new File("E:/albumList.txt"));
			BufferedWriter  bw=new BufferedWriter(fw);
			for(String obj:albumList){
				bw.write(obj+"\r\n");
			}
			bw.close();
	        fw.close();
		}catch (Exception e) {
			System.out.println(e);
	    }
        
//		AlbumService albumService = new AlbumService();
//		List<Album> list = albumService.getAlbumList("");
//		for(Album album:list){
//			System.out.println(album.getTitle()+"|"+album.getType()+"|"+album.getStarring());
//		}
//		System.out.println(list.size());
//		albumService.replaceImgSrc("xxxx<img src=\"http://static.soku.com/v1.0.0688/soku/img/favicon/tudou.png\">yyy<img src=\"http://soku/img/favicon/tudou.png\">", "cc");
//		String html = "xxxx<img src=\"http://static.soku.com/v1.0.0688/soku/img/favicon/tudou.png\">yyy";
//		html = Pattern.compile("<!img((?!<!img).)*\">", Pattern.DOTALL).matcher(html).replaceAll("找到该串后替换成其它串");
//		System.out.println("=="+html);
	}
	
	
	public PageInfo getAlbumByPage(Map searchMap,PageInfo pageInfo){
		return albumDao.getObjectPageInfo(searchMap,pageInfo);
	}
	
	public List<Album> getAlbumList(Map searchMap){
		return albumDao.getObjectList(searchMap);
	}
	
	public int saveOrUpdateAlbum(Album album){
		if(album.getId() == null){
			return albumDao.addObject(album);
		}else{
			return albumDao.updateObject(album);
		}
	}
	
	public Album getAlbumById(Long id){
		return albumDao.getObjectById(id);
	}
	
	public int deleteAlbum(String ids){
		int delNum = 0;
		if(StringUtils.isNotEmpty(ids)){
			String[] idstr = ids.split(",");
			for(int i=0;i<idstr.length;i++){
				delNum = albumDao.deleteObject(Long.valueOf(idstr[i]));
			}
		}
		return delNum;
	}
}
